package com.ip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ip.expception.ParamException;
import com.ip.mapper.IpBaseConfigMapper;
import com.ip.pojo.common.UserInfoVO;
import com.ip.pojo.model.IpBaseConfigDO;
import com.ip.pojo.model.IpConfigListVO;
import com.ip.pojo.model.UserInfoDO;
import com.ip.service.IpConfigManagerService;
import com.ip.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IpConfigManagerServiceImpl extends ServiceImpl<IpBaseConfigMapper, IpBaseConfigDO> implements IpConfigManagerService {
    private static final Logger logger = LoggerFactory.getLogger(IpConfigManagerServiceImpl.class);

    @Autowired
    private IpBaseConfigMapper ipBaseConfigMapper;
    @Autowired
    private UserService userService;

    @Override
    public List<IpConfigListVO> queryUserIpList(UserInfoVO userInfo) {
        if (userInfo == null) {
            throw new ParamException("userInfo can not be null，ip 列表查询失败！");
        }
        UserInfoDO userInfoDO = userService.queryByOpenid(userInfo.getOpenId());
        if (userInfoDO == null) {
            logger.warn("queryUserIpList 未拿到用户信息, {}", userInfo);
            return List.of();
        }
        LambdaQueryWrapper<IpBaseConfigDO> wrapper = Wrappers.lambdaQuery(IpBaseConfigDO.class);
        wrapper.eq(IpBaseConfigDO::getUserId, userInfoDO.getId());
        List<IpBaseConfigDO> ipConfigList = ipBaseConfigMapper.selectList(wrapper);
        return ipConfigList.stream().map(e -> {
            IpConfigListVO config = new IpConfigListVO();
            BeanUtils.copyProperties(e, config, IpConfigListVO.class);
            return config;
        }).toList();
    }
}
